package com.alog.yzd.bookmall.web.servlet.admin.book;

import com.alog.yzd.bookmall.domain.book.Book;
import com.alog.yzd.bookmall.domain.catagory.Catagory;
import com.alog.yzd.bookmall.pager.PageBaen;
import com.alog.yzd.bookmall.service.book.BookService;
import com.alog.yzd.bookmall.service.catagory.CatagoryService;
import com.alog.yzd.bookmall.utils.WebUtils;
import com.alog.yzd.commons.CommonUtils;
import com.alog.yzd.commons.Print;
import com.alog.yzd.commons.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/1/11 10:01
 * @Description:
 */
public class AdminBookServlet extends BaseServlet {
    private BookService bookService = new BookService();
    private CatagoryService catagoryService = new CatagoryService();

    /**
     * 删除图书
     */
    public String delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String bid = req.getParameter("bid");
        Book book = bookService.load(bid);
        String savepath = getServletContext().getRealPath("/");
        new File(savepath, book.getImage_b()).delete();
        new File(savepath, book.getImage_w()).delete();
        bookService.delete(bid);

        req.setAttribute("msg","删除图书成功!");
        return "f:/adminjsps/admin/msg.jsp";
    }
    /**
     * 编辑图书
     */
    public String edit(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Book book = CommonUtils.toBean(req.getParameterMap(), Book.class);
        Catagory catagory = CommonUtils.toBean(req.getParameterMap(), Catagory.class);
        book.setCatagory(catagory);
        bookService.edit(book);

        req.setAttribute("msg", "编辑图书成功");
        req.getRequestDispatcher("/adminjsps/admin/msg.jsp").forward(req,resp);
        return null;
    }


    public String ajaxFindCategoryChildren(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pid = req.getParameter("pid");
        List<Catagory> children = catagoryService.findChildren(pid);
        String json = toJson(children);
        Print.print("ajaxFindCategoryChildren, json : " + json);
        resp.getWriter().write(json);
        return null;
    }
    private String toJson(Catagory catagory){
        StringBuilder sb = new StringBuilder("{");
        sb.append("\"cid\"").append(":").append("\"").append(catagory.getCid()).append("\"").append(",");
        sb.append("\"cname\"").append(":").append("\"").append(catagory.getCname()).append("\"");
        sb.append("}");
        return sb.toString();
    }

    private String toJson(List<Catagory> catagories){
        StringBuilder sb = new StringBuilder("[");
        for(int i = 0 ; i < catagories.size() ; i++){
            sb.append(toJson(catagories.get(i)));
            if(i < catagories.size() - 1){
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    /**
     * 添加图书第一步
     */
    public String addPre(HttpServletRequest req, HttpServletResponse resp){
        List<Catagory> parents = catagoryService.findParents();
        req.setAttribute("parents", parents);
        return "f:/adminjsps/admin/book/add.jsp";
    }
    /**
     * 所有分类
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findCategoryAll(HttpServletRequest req, HttpServletResponse resp){
        List<Catagory> catagories = catagoryService.findAll();
        Print.print("findCategoryAll");
        req.setAttribute("parents", catagories);
        return "f:/adminjsps/admin/book/left.jsp";
    }


    /**
     * 根据书ID获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String load(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bid = req.getParameter("bid");
        Book book = bookService.load(bid);
        req.setAttribute("book", book);
        req.setAttribute("parents", catagoryService.findParents());

        String pid = book.getCatagory().getParent().getCid();
        req.setAttribute("children", catagoryService.findChildren(pid));
        req.setAttribute("parents", catagoryService.findParents());
        Print.print("load");
        return "f:/adminjsps/admin/book/desc.jsp";
    }

    /**
     * 根据书名获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByCombination(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        Book book = CommonUtils.toBean(req.getParameterMap(), Book.class);
        PageBaen<Book> pb = bookService.findByCombination(book, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByCombination, pb:" + pb);
        return "f:/adminjsps/admin/book/list.jsp";
    }
    /**
     * 根据书名获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByBname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        String bname = req.getParameter("bname");
        PageBaen<Book> pb = bookService.findByBname(bname, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByBname, pb:" + pb);
        return "f:/adminjsps/admin/book/list.jsp";
    }
    /**
     * 根据出版衬获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByPress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        String press = req.getParameter("press");
        PageBaen<Book> pb = bookService.findByPress(press, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByPress, pb:" + pb);
        return "f:/adminjsps/admin/book/list.jsp";
    }

    /**
     * 根据作者获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByAuthor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        String author = req.getParameter("author");
        PageBaen<Book> pb = bookService.findByAuthor(author, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByAuthor, pb:" + pb );
        Print.print("findByAuthor, author:" + author );
        return "f:/adminjsps/admin/book/list.jsp";
    }



    /**
     * 根据分类获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        String cid = req.getParameter("cid");
        PageBaen<Book> pb = bookService.findByCatagory(cid, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByCategory, pb:" + pb);
        return "f:/adminjsps/admin/book/list.jsp";
    }
}
